import 'dart:ui';
import 'package:boss/color_constant.dart';
import 'package:boss/models/order_detail_data.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/reportForm/order/page/orderDetail/provider/order_detail_provider.dart';
import 'package:boss/ui/widget/custom_divier.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:boss/ui/widget/loader/loader.dart';
import 'package:boss/ui/widget/shadow_radius_card.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';

import 'commodity_discount_icon.dart';
import 'order_detail_header.dart';
import 'order_detail_message.dart';

class OrderDetailBrowser extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final provider = Provider.of<OrderDetailProvider>(context, listen: false);
    return Loader<OrderDetailData>(
      loadTask: provider.queryOrderDetail,
      builder: (context) {
        return SingleChildScrollView(
          padding: EdgeInsets.only(
            left: 30.w,
            right: 30.w,
            bottom: 50.w + MediaQuery.of(context).padding.bottom,
          ),
          child: Consumer<OrderDetailProvider>(
            builder: (context, orderDetailProvider, _) {
              return Column(
                children: <Widget>[
                  OrderDetailHeader(),
                  OrderDetailMessage(
                    title: '订单信息',
                    items: {
                      '订单时间':
                          '${orderDetailProvider.data.orderProp.lockTableTime == null ? '--' : orderDetailProvider.data.orderProp.lockTableTime.substring(5)} 至 ${orderDetailProvider.data.orderProp.payTime == null ? '--' : orderDetailProvider.data.orderProp.payTime.substring(5)}',
                      '订单状态': '${orderDetailProvider.data.orderProp.orderStatusText}',
                      '桌台信息': '${orderDetailProvider.data.orderProp.tableName}',
                      '就餐人数': '${orderDetailProvider.data.orderProp.guests}',
                    },
                  ),
                  OrderDetailMessage(
                    title: '支付信息',
                    items: {
                      '订单金额': '${AmountUtil.price2String(orderDetailProvider.data.orderProp.totalPrice)}',
                      '附加费': '${orderDetailProvider.data.orderProp.surchargeText}',
                      '应收金额': '${AmountUtil.price2String(orderDetailProvider.data.orderProp.receiveAmount)}',
                      '实收金额': '${AmountUtil.price2String(orderDetailProvider.data.orderProp.totalFee)}',
                      '付款方式': '${orderDetailProvider.data.orderProp.payTypeName}',
                      '付款金额': '${AmountUtil.price2String(orderDetailProvider.data.orderProp.totalFee + orderDetailProvider.data.orderProp.chargeMoney)}',
                      '找零金额': '${AmountUtil.price2String(orderDetailProvider.data.orderProp.chargeMoney)}',
                      '优惠方式': orderDetailProvider.data.orderProp.orderDiscountText,
                      '优惠合计': '${AmountUtil.price2String(orderDetailProvider.data.orderProp.couponFee)}',
                      '抹零合计': '${AmountUtil.price2String(orderDetailProvider.data.orderProp.sysDecreaseAmount)}',
                      '减免金额': '${AmountUtil.price2String(orderDetailProvider.data.orderProp.decreaseAmount)}',
                      '退单金额': '${AmountUtil.price2String(orderDetailProvider.data.orderProp.withDrawAmount)}',
                      '小费金额': '${AmountUtil.price2String(orderDetailProvider.data.orderProp.tip)}',
                    },
                  ),
                  OrderDetailMessage(
                    title: '备注信息',
                    items: {
                      '订单备注': '${orderDetailProvider.data.orderProp.remark}',
                      '免单备注': '${orderDetailProvider.data.orderProp.freeReason}',
                      '退单备注': '${orderDetailProvider.data.orderProp.withDrawReason}',
                      '反结备注': '${orderDetailProvider.data.orderProp.oppsiteOrderReason}',
                      '操作人': '${orderDetailProvider.data.orderProp.placeOrderMan ?? '--'}',
                      '操作时间': '${(orderDetailProvider.data.orderProp.oppositeOrderTime==null || orderDetailProvider.data.orderProp.oppositeOrderTime=='')?'--':DateTime.parse(orderDetailProvider.data.orderProp.oppositeOrderTime).toString().substring(0,19)}',
                      '用餐评价': '${orderDetailProvider.data.orderProp.waiterRateDesc}',
                    },
                  ),
                  OrderDetailMessage(
                    title: '会员信息',
                    items: {
                      '会员卡号': '',
                      '会员储值': '',
                    },
                  ),
                  OrderDetailMessage(
                    title: '发票信息',
                    items: {
                      '发票类型': '',
                      '发票抬头': '',
                      '发票金额': '',
                    },
                  ),
                  ShadowRadiusCard(
                    child: Column(
                      children: <Widget>[
                        Container(
                          height: 90.w,
                          decoration: BoxDecoration(
                            color: ColorConstant.ffcc.withOpacity(0.2),
                            borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(14.w),
                              topRight: Radius.circular(14.w),
                            ),
                          ),
                          child: DefaultTextStyle(
                            style: TextStyleConstant().normal_2_28,
                            textAlign: TextAlign.center,
                            child: Row(
                              children: <Widget>[
                                Expanded(child: Text('菜品名称')),
                                Expanded(child: Text('单价')),
                                Expanded(child: Text('数量')),
                                Expanded(child: Text('金额')),
                              ],
                            ),
                          ),
                        ),
                        Column(
                          children: orderDetailProvider.carts.map((item) {
                            final parent = item.parent;

                            TextStyle textStyle;
                            if (parent.isBack == 1) {
                              textStyle = TextStyleConstant().normal_2_28;
                            } else if (parent.commodityType == 3 && parent.porderId != 0) {
                              textStyle = TextStyleConstant().normal_2_24;
                            } else {
                              textStyle = TextStyleConstant().normal_1_28;
                            }

                            return DefaultTextStyle(
                              style: textStyle,
                              textAlign: TextAlign.center,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              child: Stack(
                                overflow: Overflow.visible,
                                children: <Widget>[
                                  Container(
                                    padding: EdgeInsets.symmetric(vertical: 15.w),
                                    child: Row(
                                      children: <Widget>[
                                        Expanded(
                                          flex: 3,
                                          child: Column(
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            children: <Widget>[
                                              Row(
                                                children: <Widget>[
                                                  Expanded(
                                                    child: Padding(
                                                      padding: EdgeInsets.only(
                                                        left: 20.w,
                                                      ),
                                                      child: Row(
                                                        children: <Widget>[
                                                          Expanded(
                                                            child: Text(
                                                              parent.title,
                                                              textAlign: TextAlign.start,
                                                            ),
                                                          ),
                                                          CommodityDiscountIcon(parent.discountType, parent.commodityType),
                                                        ],
                                                      ),
                                                    ),
                                                  ),
                                                  Expanded(child: Text('${AmountUtil.price2String(parent.price)}')),
                                                  Expanded(child: Text('${parent.num}')),
                                                ],
                                              ),
                                              DefaultTextStyle(
                                                style: TextStyleConstant().normal_2_24,
                                                child: Container(
                                                  padding: EdgeInsets.only(
                                                    left: 20.w,
                                                  ),
                                                  child: Column(
                                                    crossAxisAlignment: CrossAxisAlignment.start,
                                                    children: <Widget>[
                                                      if (parent.orderDetailMeterialList.length > 0)
                                                        Container(
                                                          margin: EdgeInsets.only(top: 15.w),
                                                          child: Text(parent.materialText),
                                                        ),
                                                      if (parent.orderDetailTasteList.length > 0)
                                                        Container(
                                                          margin: EdgeInsets.only(top: 15.w),
                                                          child: Text(parent.tasteText),
                                                        ),
                                                      if (parent.remark != null && parent.remark != '')
                                                        Container(
                                                          margin: EdgeInsets.only(top: 15.w),
                                                          child: Text(parent.remark),
                                                        ),
                                                      //打折原因
                                                      if(parent.discountReason!=null && parent.discountReason!='')
                                                      Text("打折原因：${parent.discountReason ?? '--'}"),
                                                    ],
                                                  ),
                                                ),
                                              ),
                                              for (final child in item.children)
                                                Column(
                                                  crossAxisAlignment: CrossAxisAlignment.start,
                                                  children: <Widget>[
                                                    DefaultTextStyle(
                                                      style: TextStyleConstant().normal_2_24,
                                                      child: Container(
                                                        padding: EdgeInsets.symmetric(
                                                          vertical: 15.w,
                                                        ),
                                                        child: Row(
                                                          children: <Widget>[
                                                            Expanded(
                                                              flex: 2,
                                                              child: Padding(
                                                                padding: EdgeInsets.only(
                                                                  left: 20.w,
                                                                ),
                                                                child: Row(
                                                                  children: <Widget>[
                                                                    Text(
                                                                      child.title,
                                                                      textAlign: TextAlign.start,
                                                                    ),
                                                                  ],
                                                                ),
                                                              ),
                                                            ),
                                                            Expanded(
                                                                child: Text(
                                                              '${child.num}',
                                                              textAlign: TextAlign.center,
                                                            )),
                                                          ],
                                                        ),
                                                      ),
                                                    ),
                                                    DefaultTextStyle(
                                                      style: TextStyleConstant().normal_2_24,
                                                      child: Container(
                                                        padding: EdgeInsets.only(
                                                          left: 20.w,
                                                        ),
                                                        child: Column(
                                                          crossAxisAlignment: CrossAxisAlignment.start,
                                                          children: <Widget>[
                                                            if (child.orderDetailMeterialList.length > 0)
                                                              Container(
                                                                margin: EdgeInsets.only(top: 15.w),
                                                                child: Text(child.materialText),
                                                              ),
                                                            if (child.orderDetailTasteList.length > 0)
                                                              Container(
                                                                margin: EdgeInsets.only(top: 15.w),
                                                                child: Text(child.tasteText),
                                                              ),
                                                            if (child.remark != null && child.remark != '')
                                                              Container(
                                                                margin: EdgeInsets.only(top: 15.w),
                                                                child: Text(child.remark),
                                                              ),
                                                          ],
                                                        ),
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                            ],
                                          ),
                                        ),
                                        Expanded(
                                          child: Row(
                                            mainAxisAlignment: MainAxisAlignment.center,
                                            children: <Widget>[
                                              Text('${AmountUtil.price2String(parent.actualPrice)}'),
                                              if (parent.actualPrice < parent.price && parent.actualPrice > 0)
                                                Text(
                                                  '(${(parent.actualPrice / parent.price * 10).toStringAsFixed(1)}折)',
                                                  style: TextStyleConstant().normal_2_24,
                                                ),
                                            ],
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                  if (parent.isBack == 1)
                                    Positioned(
                                      top: -15.w,
                                      right: 20.w,
                                      child: BaseImage(
                                        'assets/image/goods_back.png',
                                        width: 132.w,
                                      ),
                                    ),
                                  Positioned(
                                    bottom: 0,
                                    left: 20.w,
                                    right: 20.w,
                                    child: CustomDivider(),
                                  ),
                                ],
                              ),
                            );
                          }).toList(),
                        )
                      ],
                    ),
                  ),
                ],
              );
            },
          ),
        );
      },
    );
  }
}
